package com.lianxian.xindong.old.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.lianxian.xindong.old.R;
import com.lianxian.xindong.old.adapter.PagerAdapter;
import com.lianxian.xindong.old.fragment.home.DynamicFragment;
import com.lianxian.xindong.old.fragment.home.HobbyFragment;
import com.lianxian.xindong.old.fragment.home.LocalLifeFragment;
import com.lianxian.xindong.old.fragment.home.SelfieFragment;
import com.lianxian.xindong.old.fragment.home.SingleTribeFragment;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private TabLayout mTabLayout;
    private ViewPager2 mViewPager;
    private FragmentStateAdapter mFragmentsAdapter;
    String[] tabNames = new String[]{"自拍","动态","兴趣爱好","单身部落","本地生活"};
    List<Fragment> fragments = new ArrayList<>();
    private TabLayoutMediator mediator;
    public HomeFragment() {
        // Required empty public constructor
    }
    FragmentManager childFragmentManager;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home2, container, false);
        mTabLayout = root.findViewById(R.id.tabLayout);
        mViewPager = root.findViewById(R.id.viewPager);
        int screenHieght = getScreenHeightOrWidthPX(getActivity(),1);
        ViewPager2.LayoutParams layoutParams = mViewPager.getLayoutParams();
        layoutParams.height = screenHieght-dip2px(getActivity(),40)-getStatusBarHeight();
        mViewPager.setLayoutParams(layoutParams);


        fragments.add(new SelfieFragment());
        fragments.add(new DynamicFragment());
        fragments.add(new HobbyFragment());
        fragments.add(new SingleTribeFragment());
        fragments.add(new LocalLifeFragment());

        for (int i=0;i<tabNames.length;i++){
            mTabLayout.addTab(mTabLayout.newTab());
        }
        childFragmentManager = getParentFragmentManager();
        mFragmentsAdapter = new PagerAdapter(childFragmentManager,getLifecycle(),fragments);
        mViewPager.setAdapter(mFragmentsAdapter);
        mediator = new TabLayoutMediator(mTabLayout, mViewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {

            }
        });
        mediator.attach();
        for (int i=0;i<tabNames.length;i++){
            mTabLayout.getTabAt(i).setText(tabNames[i]);
        }
        return root;
    }
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        int x = (int) (pxValue / scale + 0.5f);
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    public int getScreenHeightOrWidthDP(Activity context, int flag) {
        DisplayMetrics DM = new DisplayMetrics();
        Display display = context.getWindowManager().getDefaultDisplay();
        if (Build.VERSION.SDK_INT < 17)
            display.getMetrics(DM);
        else
            display.getRealMetrics(DM);

        if (flag==0){
            return px2dip(context,DM.widthPixels);
        }else if (flag==1){
            return px2dip(context,DM.heightPixels);
        }
        return -1;
    }
    public int getScreenHeightOrWidthPX(Activity context, int flag) {
        DisplayMetrics DM = new DisplayMetrics();
        Display display = context.getWindowManager().getDefaultDisplay();
        if (Build.VERSION.SDK_INT < 17)
            display.getMetrics(DM);
        else
            display.getRealMetrics(DM);

        if (flag==0){
            return DM.widthPixels;
        }else if (flag==1){
            return DM.heightPixels;
        }
        return -1;
    }
    //获取状态栏高度
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}